﻿using System;
using System.Collections.Generic;
using System.Windows;

namespace xsb
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>

    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        private void path_select(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    filepath.Text = openFileDialog.FileName;
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }

        }

        private void gen(object sender, RoutedEventArgs e)
        {
            string group_count_str = group_count.Text;
            int group_count_int;

            try
            {
                group_count_int = Convert.ToInt32(group_count_str);
            }catch(Exception)
            {
                MessageBox.Show("输入的组数不合法");
                return;
            }

            if (core.XlsUtil.decodeFromXls(filepath.Text))
            {
                MessageBox.Show("读取文件时发生了错误");
                return;
            }

            List<List<core.Data.Group>> sheets = new List<List<core.Data.Group>>();
            string result_count_str = result_count.Text;
            int result_count_int;
            try
            {
                result_count_int = Convert.ToInt32(result_count_str);
            }
            catch (Exception)
            {
                MessageBox.Show("输入的所需结果数不合法");
                return;
            }


            while (result_count_int-- != 0)
                sheets.Add(core.Algorithms.run_once(group_count_int,(int)bad_factor.Value));


            core.XlsUtil.encodeToXls(sheets, System.IO.Path.GetDirectoryName(filepath.Text)
                +"\\"+ System.IO.Path.GetFileNameWithoutExtension(filepath.Text) + "_已编排.xlsx");
        }
    }
}
